<?php
// +----------------------------------------------------------------------
// | Site:  [ http://www.yzmcms.com]
// +----------------------------------------------------------------------
// | Copyright: 袁志蒙工作室，并保留所有权利
// +----------------------------------------------------------------------
// | Author: YuanZhiMeng <214243830@qq.com>
// +---------------------------------------------------------------------- 
// | Explain: 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用，不允许对程序代码以任何形式任何目的的再发布！
// +----------------------------------------------------------------------

defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_sys_class('page','',0);
yzm_base::load_common('class/jst.class.php');
yzm_base::load_common('class/xlswriter.class.php');

class jst_sku extends common {

	/**
	 * 配置信息
	 */

	public $config=[];
	public $jst=[];
	public $jst_arr=[];
	public $data=[];
	
	public function __construct(){
		//聚水潭列表
		$this->jst = get_jst_list();
		$k = 0;
		foreach($this->jst as $v){
			$this->jst_arr[$k]['id'] = $v['id'];
			$this->jst_arr[$k]['title'] = $v['short_name'];
			$k++;
		}
		parent::__construct();
	}
	
	public function where(){
		$enabled = $_POST["enabled"] ?? '';
		$is_syn = $_POST["is_syn"] ?? '';
		$brand = isset($_POST["brand"]) ? $_POST["brand"] : '';
		$category = isset($_POST["category"]) ? $_POST["category"] : '';
		$supplier_name = isset($_POST["supplier_name"]) ? $_POST["supplier_name"] : '';
		$i_id = isset($_POST["i_id"]) ? $_POST["i_id"] : '';
		$sku_id = isset($_POST["sku_id"]) ? $_POST["sku_id"] : '';
		$sku_code = isset($_POST["sku_code"]) ? $_POST["sku_code"] : '';
		$other_2 = $_POST["other_2"] ?? '';
		$properties_value = isset($_POST['properties_value']) ? safe_replace(trim($_POST['properties_value'])) : '';
		$name = isset($_POST["name"]) ? $_POST["name"] : '';
		$type = input('get.type') ?? '';
		$c_id = isset($_POST["c_id"]) ? $_POST["c_id"] : '';
		$where = '1=1';
		
		if($i_id) {
			if(strpos($i_id,',')){
				$i_id = str_replace(',','","',$i_id);
				$where .= ' AND i_id in("'.$i_id.'")';
			}else{
				if($i_id != trim($i_id,'*')){
					$i_id = trim($i_id,'*');
					$where .= ' AND i_id LIKE \'%'.$i_id.'%\'';
				}else{
					$where .= ' AND i_id = "'.$i_id.'"';
				}
			}
		}
		if($sku_id) {
			if(strpos($sku_id,',')){
				$sku_id = str_replace(',','","',$sku_id);
				$where .= ' AND sku_id in("'.$sku_id.'")';
			}else{
				if($sku_id != trim($sku_id,'*')){
					$sku_id = trim($sku_id,'*');
					$where .= ' AND sku_id LIKE \'%'.$sku_id.'%\'';
				}else{
					$where .= ' AND sku_id = "'.$sku_id.'"';
				}
			}
		}
		if($sku_code) {
			$where .= ' AND sku_code = "'.$sku_code.'"';
		}
		if($properties_value) {
			$where .= ' AND properties_value LIKE \'%'.$properties_value.'%\'';
		}
		if($name) {
			$where .= ' AND name LIKE \'%'.$name.'%\'';
		}
		if($other_2) {
			if(strpos($other_2,',')){
				$other_2 = str_replace(',','","',$other_2);
				$where .= ' AND other_2 in("'.$other_2.'")';
			}else{
				$where .= ' AND other_2 LIKE \'%'.$other_2.'%\'';
			}
		}
		if($c_id) {
			$where .= ' AND c_id ='.$c_id;
		}
		if($enabled!='') {
			$where .= ' AND enabled = '.$enabled;
		}
		if($is_syn!='') {
			if($is_syn == 1){
				$where .= ' AND inventory_syn_time > 0';
			}elseif($is_syn == 2){
				$where .= ' AND inventory_syn_time = 0';
			}elseif($is_syn == 3){
				$where .= ' AND add_time =0';
			}elseif($is_syn == 4){
				$where .= ' AND add_time >0';
			}	
		}
		if($brand){
			$brand = str_replace(',','","',$brand);
			$where .= ' AND brand in("'.$brand.'")';
		}
		if($category){
			$category = str_replace(',','","',$category);
			$where .= ' AND category in("'.$category.'")';
		}
		if($supplier_name){
			$supplier_name = str_replace(',','","',$supplier_name);
			$where .= ' AND supplier_name in("'.$supplier_name.'")';
		}
		return $where;
	}
	
	public function sku_list() {
		$of = input('post.field') ? input('post.field') : 'modified';
		$or = input('post.order') ? input('post.order') : 'desc';
		$download = isset($_POST['download']) ? $_POST['download'] : 0;
		$page = input('post.page');
		$limit = input('post.limit');
		$where = $this->where();
		$sku = D('jst_sku_sales');
		if($page&&$download==0){
			$total = $sku->where($where)->total();
			$page = new page($total, $limit);
			$data = $sku->order("$of $or")->where($where)->limit($page->limit())->order("$of $or")->select();
			//echo $sku->lastsql();exit;
			foreach($data as &$v){
				if(!$v['pic']){
					$v['pic'] = STATIC_URL.'/images/nopic.jpg';
				}
				$v['jst_name'] = $this->jst[$v['jst_id']]['short_name'];
				$v['jst_name2'] = $this->jst[$v['jst_id2']]['short_name'];
			}
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$data];
			return_json($data);
		}

		if($download==1){
			if(!$total = getcache('JST_DOWNLOAD::total_'.$_SESSION['adminid'])){
				$total = $sku->where($where)->total();
				setcache('JST_DOWNLOAD::total_'.$_SESSION['adminid'], $total);
			}
			$number = 50000;
			if($total>50000){
				$page = ceil($total/$number);
				if(!$this_page = getcache('JST_DOWNLOAD::this_page_'.$_SESSION['adminid'])){
					$this_page = 1;
					delete_all('JST_DOWNLOAD:*');
				}
				$w = getcache('JST_DOWNLOAD::where_'.$_SESSION['adminid']);
				if($w){
					$where = $w;
				}else{
					setcache('JST_DOWNLOAD::where_'.$_SESSION['adminid'], $where);
				}
				$limit = ($this_page-1)*$number.','.$number;
				$fields = excel_need_field('sku');
				$field = implode(',',$fields);
				$data = $sku->field($field)->where($where)->order("$of $or")->limit($limit)->select();
				setcache('JST_DOWNLOAD::this_page_'.$_SESSION['adminid'], $this_page+1);
				setcache('JST_DOWNLOAD::data_'.$_SESSION['adminid'].'_'.$this_page, $data);
				if($this_page < $page){
					$data = ['code'=>2,'msg'=>'成功加载'.$this_page.'数据，继续加载'.time()];
					return_json($data);
				}else{
					delcache('JST_DOWNLOAD::this_page_'.$_SESSION['adminid']);
					delcache('JST_DOWNLOAD::where_'.$_SESSION['adminid']);
					$excel = new xlswriter('sku',1);
					for($i=1;$i<=$page;$i++){
						$excel->insert_data(getcache('JST_DOWNLOAD::data_'.$_SESSION['adminid'].'_'.$i));
						delcache('JST_DOWNLOAD::data_'.$_SESSION['adminid'].'_'.$i);
					}
					$url = $excel->output();
					$data = ['code'=>0,'msg'=>'','url'=>$url];
					return_json($data);
					$data = ['code'=>3,'msg'=>'数据加载成功开始开出'];
					return_json($data);
				}
				//$data = ['code'=>1,'msg'=>'单次最多导出5万条记录','url'=>$url];
				//return_json($data);
			}else{
				$excel = new xlswriter('sku',1);
				$fields = excel_need_field('sku');
				$field = implode(',',$fields);
				$data = $sku->field($field)->where($where)->order("$of $or")->select();
				$excel->insert_data($data);
				unset($data);
				$url = $excel->output();
				$data = ['code'=>0,'msg'=>'','url'=>$url];
				return_json($data);
			}
		}else{
			include $this->admin_tpl('jst_sku/sku_list');
		}		
	}
	
	//重新拉取商品资料
	public function reget_sku(){
		$where = $this->where();
		$ids = $_POST['ids']??[];
		if($ids){
			$where = 'id in ('.implode(',',$ids).')';
		}
		$total = D('jst_sku')->where($where)->total();
		$res = D('jst_sku')->update(['add_time'=>0],$where);
		if($res!==false){
			$msg = '预计：'.ceil($total/(20*8*5)).'分钟处理完成';
			$data = ['code'=>0,'msg'=>$msg];
			return_json($data);
		}else{
			$data = ['code'=>0,'msg'=>'操作失败！'];
			return_json($data);
		}
	}
	
	//重新拉取商品库存
	public function reget_inventory(){
		$where = $this->where();
			$ids = $_POST['ids']??[];
			if($ids){
				$where = 'id in ('.implode(',',$ids).')';
			}
			$total = D('jst_sku')->where($where)->total();
			$res = D('jst_sku')->update(['inventory_syn_time'=>0],$where);
			if($res!==false){
				$msg = '预计：'.ceil($total/(20*8*5)).'分钟处理完成';
				$data = ['code'=>0,'msg'=>$msg];
				return_json($data);
			}else{
				$data = ['code'=>0,'msg'=>'操作失败！'];
				return_json($data);
			}
	}
	
}